home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 152 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  66 lines

  1. Path: lrz-muenchen.de!sun2!ua302aa
  2. From: ua302aa@sun2.lrz-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: help!: An array of strings
  5. Date: 2 Jan 1996 11:45:42 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4cb5t6$onb@sparcserver.lrz-muenchen.de>
  9. References: <Pine.SUN.3.91.960101231746.4185A-100000@parsifal.nando.net>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. pretzel <pretzel@nando.net> writes:
  13.  
  14. >Is there any way to create and use an array of strings?  I am trying to 
  15. >create an adventure game that gets its descriptions from an array.  In 
  16. >BASIC the code looks like this....
  17.  
  18. [BASIC code edited]
  19.  
  20. >access them.  That is how I do it in BASIC, but I can't figure it out in 
  21. >C, since 'char rooms[50][2]' defines an array of single characters.  One 
  22. >element in the array might be "You are in a dark alley which reeks of 
  23. >garbage.  You feel nervous." so THAT approach certainly is not what I'm 
  24. >looking for.    
  25.  
  26. 1) Yes, there is a way to create and use an array of strings. The 
  27.    problem is that C has no built-in sting type like other languages.
  28.    In C, a string is a consecutive area of memory with a sequence
  29.    of characters that is terminated by '\0'.
  30.  
  31.    So, the real problem is how to represent strings in C. There are
  32.    two approaches that are somewhat "natural" and dozends of other
  33.    ones that have advantages and disadvantages in certain situations.
  34.  
  35. 2) Use either
  36.  
  37.    char *rooms[50][2];
  38.  
  39.    of
  40.  
  41.    char rooms[50][2][128];
  42.  
  43.    two declare an array of arrays of either pointers to char
  44.    or arrays of char.
  45.  
  46.    In the first case, you can easily assign "string constants" to
  47.    the elements of your array:
  48.  
  49.       rooms[32][0] = "You are somewhere and it looks somehow";
  50.  
  51.    In the second case, the arrays of char can be used to store the
  52.    strings, and you can modify them if you have to:
  53.  
  54.       strcpy(rooms[49][1], "You feel somehow");
  55.  
  56.       /* and later: */
  57.  
  58.       strcpy(strstr(rooms[49][1], "somehow"), "dizzy");
  59.  
  60. Kurt
  61. --
  62. | Kurt Watzka                             Phone : +49-89-2180-6254
  63. | watzka@stat.uni-muenchen.de
  64. | ua302aa@sunmail.lrz-muenchen.de
  65.  
  66.